<?php

declare (strict_types=1);
namespace App\Model;

use App\Traits\ModelLabelTrait;
use Hyperf\Database\Model\SoftDeletes;
use Hyperf\DbConnection\Model\Model;
/**
 * @property int $id 
 * @property int $user_id 
 * @property string $true_name 
 * @property int $sex 
 * @property string $birthday 
 * @property string $id_card 
 * @property int $user_type 
 * @property string $house_province_name 
 * @property string $house_city_name 
 * @property string $house_district_name 
 * @property string $house_address 
 * @property string $person_sign 
 * @property string $life_photo 
 * @property \Carbon\Carbon $created_at 
 * @property \Carbon\Carbon $updated_at 
 * @property string $deleted_at 
 */
class UserInfo extends Model
{
    use SoftDeletes,ModelLabelTrait;
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'user_info';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'user_id',
        'true_name',
        'sex',
        'birthday',
        'id_card',
        'user_type',
        'house_address',
        'person_sign',
        'life_photo'
    ];

    protected $appends=[
        'sex_label',
        'user_type_label',
        'age',
        'life_photo_url'
    ];
    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = ['id' => 'int', 'user_id' => 'integer', 'sex' => 'integer', 'user_type' => 'integer', 'created_at' => 'datetime', 'updated_at' => 'datetime'];

    const SEX_MAN = 1;
    const SEX_WOMAN = 2;
    const SEX_LABELS=[
        self::SEX_MAN => '男',
        self::SEX_WOMAN => '女'
    ];

    const USER_TYPE_ONE =  1;
    const USER_TYPE_TWO =  2;
    const USER_TYPE_THREE =  3;
    const USER_TYPE_FOUR =  4;

    const USER_TYPE_LABELS=[
        self::USER_TYPE_ONE => '学生',
        self::USER_TYPE_TWO => '应届',
        self::USER_TYPE_THREE => '在职',
        self::USER_TYPE_FOUR => '离职'
    ];

    public function getSexLabelAttribute(){
        return self::getLabel('sex',$this->sex);
    }

    public function getUserTypeLabelAttribute(){
        return self::getLabel('user_type',$this->user_type);
    }

    public function getAgeAttribute(){
        if($this->birthday){
            $year = explode('-',$this->birthday)[0];
            return date('Y')-$year;
        }
        return 0;
    }

    public function getLifePhotoUrlAttribute(){
        return $this->life_photo?config('common.image_prefix').$this->life_photo:null;
    }
}
